<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("components4phpfull/jtiframe.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class vendor_create extends Page
{
       public $imgBack = null;
       public $hiddenVendor = null;
       public $hiddenNewCount = null;
       public $hiddenExist = null;
       public $cmbContactMunicipality = null;
       public $cmbContactProvince = null;
       public $cmbContactRegion = null;
       public $edContactBldg = null;
       public $edContactAddress = null;
       public $btnCancelContact = null;
       public $btnSubmitContact = null;
       public $edEmail = null;
       public $Label30 = null;
       public $edFax = null;
       public $Label29 = null;
       public $Label28 = null;
       public $Label27 = null;
       public $Label26 = null;
       public $Label25 = null;
       public $Label24 = null;
       public $edMobile = null;
       public $Label23 = null;
       public $edPhone = null;
       public $Label22 = null;
       public $cmbMonth = null;
       public $cmbDay = null;
       public $Label21 = null;
       public $cmbyear = null;
       public $edContactDesignation = null;
       public $Label20 = null;
       public $edContactLname = null;
       public $Label19 = null;
       public $edContactMname = null;
       public $Label12 = null;
       public $edContactFname = null;
       public $Label10 = null;
       public $JTDivWindow1 = null;
       public $JTContactList = null;
       public $edCompEmail = null;
       public $edCompFax = null;
       public $edCompPhone = null;
       public $edOtherProd = null;
       public $cmbProduct = null;
       public $edBldg = null;
       public $Label8 = null;
       public $JTSiteTheme1 = null;
       public $Button1 = null;
       public $Label18 = null;
       public $Label17 = null;
       public $cmbMunicipality = null;
       public $cmbProvince = null;
       public $cmbRegion = null;
       public $Label16 = null;
       public $Label15 = null;
       public $Label5 = null;
       public $Image1 = null;
       public $chkinactive = null;
       public $btnCancel = null;
       public $btnUpdate = null;
       public $edAddress = null;
       public $edVendorname = null;
       public $edVendornum = null;
       public $Label7 = null;
       public $Label6 = null;
       public $Label4 = null;
       public $Label3 = null;
       public $Label2 = null;
       public $Label1 = null;
       public $lblVendorNum = null;

       function imgBackClick($sender, $params)
       {
       $query_delete_contact = mysql_query("delete from vendor_contact where company_id='".$_SESSION['company_id']."' and vendor_num='' and edit_who='".$_SESSION['username']."'");
       redirect('vendor_list.php?company_id='.$_SESSION['company_id']);
       }

       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
        document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
        document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

       function cmbProvinceJSChange($sender, $params)
       {
       echo $this->cmbProvince->ajaxCall("VendorProvinceChange");
       ?>
       return false;

       <?php

       }

       function cmbRegionJSChange($sender, $params)
       {
       echo $this->cmbRegion->ajaxCall('VendorRegionChange');
       ?>
       return false;
       <?php

       }

       function cmbContactProvinceJSChange($sender, $params)
       {
       echo $this->cmbContactMunicipality->ajaxCall("ProvinceChange");
       ?>
       //Add your javascript code here
       return false;
       <?php

       }

       function cmbContactRegionJSChange($sender, $params)
       {
       echo $this->cmbContactProvince->ajaxCall("RegionChange");
       ?>
       return false;
       <?php

       }



       function RegionChange()
       {
       $region_id = $this->cmbContactRegion->getItemIndex();
       $this->cmbContactProvince->Clear();
       $this->cmbContactProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbContactProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       if($region_id==0)
       {
        $this->cmbContactProvince->Clear();
        $this->cmbContactMunicipality->Clear();
        $this->cmbContactProvince->AddItem('--Select Region First--',null,null);
        $this->cmbContactMunicipality->AddItem('--Select Province First--',null,null);
       }
       }


       function ProvinceChange()
       {
       $region_id = $this->cmbContactRegion->getItemIndex();
       $this->cmbContactProvince->Clear();
       $this->cmbContactProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbContactProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       $province_id = $this->cmbContactProvince->getItemIndex();
       $this->cmbContactMunicipality->Clear();
       $this->cmbContactMunicipality->AddItem('--Select--', null, null);
       $result_municipality = mysql_query("select * from lookup_municipality where province_id='".$province_id."'");

       while($row_municipality = mysql_fetch_array($result_municipality))
       {
            $this->cmbContactMunicipality->AddItem($row_municipality['municipality'], null, $row_municipality['id']);
       }

       if($province_id==0)
       {
        $this->cmbContactMunicipality->Clear();
        $this->cmbContactMunicipality->AddItem('--Select Province First--',null,null);
       }
       }



       function btnCancelContactJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivWindow1').Hide();
       return false;
       <?php

       }


       function btnSubmitContactJSClick($sender, $params)
       {
       echo $this->btnSubmitContact->ajaxCall("insertContact");
       ?>
       alert('Added contact successfully');
       document.getElementById('JTDivWindow1').Hide();
       document.getElementById("JTContactList").src="vendor_contact_list.php?company_id=+$_SESSION['company_id']&vendor_id=+$this->hiddenVendor->getValue()"
       return(false);
       <?php
       }

       function Button1JSClick($sender, $params)
       {

       ?>
       window.open("vendor_contact_create.php","Window1",  "menubar=no,width=790,height=320,left=300,top=230,toolbar=no");

       return false;
       <?php

       }


       function btnCancelContactClick($sender, $params)
       {
       $this->JTDivWindow1->setVisible(false);
       }


       function insertContact()
       {
       mysql_query("INSERT INTO vendor_contact(company_id,
       fname,
       mname,
       lname,
       designation,
       bdyear,
       bdday,
       bdmonth,
       phone,
       mobile,
       address,
       bldg,
       region,
       province,
       municipality,
       fax,
       email,
       edit_who)

       VALUES
       ('".$_SESSION['company_id']."',
       '".$this->edContactFname->Text."',
       '".$this->edContactMname->Text."',
       '".$this->edContactLname->Text."',
       '".$this->edContactDesignation->Text."',
       '".$this->cmbyear->getItemIndex()."',
       '".$this->cmbDay->getItemIndex()."',
       '".$this->cmbMonth->getItemIndex()."',
       '".$this->edPhone->Text."',
       '".$this->edMobile->Text."',
       '".$this->edContactAddress->Text."',
       '".$this->edContactBldg->Text."',
       '".$this->cmbContactRegion->getItemIndex()."',
       '".$this->cmbContactProvince->getItemIndex()."',
       '".$this->cmbContactMunicipality->getItemIndex()."',
       '".$this->edFax->Text."',
       '".$this->edEmail->Text."',
       '".$_SESSION['username']."')") or die("INSERT CONTACT - ".mysql_error());


       }







       function btnCancelClick($sender, $params)
       {
       $query_delete_contact = mysql_query("delete from vendor_contact where company_id='".$_SESSION['company_id']."' and vendor_num='' and edit_who='".$_SESSION['username']."'");
       redirect('vendor_list.php?company_id='.$_SESSION['company_id']);
       }

       function vendor_createCreate($sender, $params)
       {
       $this->hiddenVendor->setValue(0);
       //start auto-generate vin
       //hiddenExist
       //hiddenNewCount
       $concat_string = 'Vend#';
       $result_vendor_count = mysql_query("SELECT * FROM lookup_vendor_count WHERE company_id='".$_SESSION['company_id']."'")
       or die("vendor count ".mysql_error());

       if(mysql_num_rows($result_vendor_count)==0)
       {
        $this->edVendornum->Text = $concat_string.'000001';
        $this->hiddenExist->setValue('0');
       }
       if(mysql_num_rows($result_vendor_count)>0)
       {
        $row_vendor_count = mysql_fetch_array($result_vendor_count);
        $number = $row_vendor_count['total_vendor_count']+1;

        $this->hiddenNewCount->setValue($number);

        $length = strlen($number);
        $num_zero = 6 - $length;
        $zero = '';
        while($num_zero>0)
         {
           $zero = $zero.'0';
           $num_zero--;
         }
        $this->edVendornum->Text = $concat_string.$zero.$number;
        $this->hiddenExist->setValue('1');
       }
       //end auto-generate vin









        $this->cmbProduct->Clear();
        $this->cmbProduct->AddItem('--Select--', null, null);
        $result_style = mysql_query("select * from lookup_inventory_tbl where category='types'");

        while($row_style = mysql_fetch_array($result_style))
        {
            $this->cmbProduct->AddItem($row_style['value'], null, $row_style['value']);
        }

        $this->JTContactList->setURL('vendor_contact_list.php?company_id='.$_SESSION['company_id'].'&vendor_id='.$this->hiddenVendor->getValue());



       //$this->edVendornum->Text='';
       $this->edVendorname->Text='';
       $this->edAddress->Text='';
       $this->edBldg->Text='';
       $this->edOtherProd->Text='';
       $this->edCompPhone->Text='';
       $this->edCompFax->Text='';
       $this->edCompEmail->Text='';


       //Contact region,prov,municipality

       $this->cmbContactRegion->Clear();
       $this->cmbContactRegion->AddItem('--Select--', null, null);
       $result_region = mysql_query("select * from lookup_region");

        while($row_region = mysql_fetch_array($result_region))
        {
            $this->cmbContactRegion->AddItem($row_region['region'], null, $row_region['id']);
        }
        $this->cmbContactProvince->Clear();
        $this->cmbContactMunicipality->Clear();
        $this->cmbContactProvince->AddItem('--Select Region First--',null,null);
        $this->cmbContactMunicipality->AddItem('--Select Province First--',null,null);



       //Vendor Region,Prov, Municipality

       $this->cmbRegion->Clear();
       $this->cmbRegion->AddItem('--Select--', null, null);
       $result_vend_region = mysql_query("select * from lookup_region");

        while($row_vend_region = mysql_fetch_array($result_vend_region))
        {
            $this->cmbRegion->AddItem($row_vend_region['region'], null, $row_vend_region['id']);
        }

        $this->cmbProvince->Clear();
        $this->cmbMunicipality->Clear();
        $this->cmbProvince->AddItem('--Select Region First--',null,null);
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }




       function VendorRegionChange()
       {
       $region_id = $this->cmbRegion->getItemIndex();
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       if($region_id==0)
       {
        $this->cmbProvince->Clear();
        $this->cmbMunicipality->Clear();
        $this->cmbProvince->AddItem('--Select Region First--',null,null);
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }
       }


       function VendorProvinceChange()
       {
       $region_id = $this->cmbRegion->getItemIndex();
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       $province_id = $this->cmbProvince->getItemIndex();
       $this->cmbMunicipality->Clear();
       $this->cmbMunicipality->AddItem('--Select--', null, null);
       $result_municipality = mysql_query("select * from lookup_municipality where province_id='".$province_id."'");

       while($row_municipality = mysql_fetch_array($result_municipality))
       {
            $this->cmbMunicipality->AddItem($row_municipality['municipality'], null, $row_municipality['id']);
       }

       if($province_id==0)
       {
        $this->cmbMunicipality->Clear();
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }
       }









       function btnUpdateClick($sender, $params)
       {
       if($this->chkinactive->Checked==true)
       {
       $inactive='1';
       }
       else
       {
       $inactive='0';
       }

       mysql_query("INSERT INTO vendor_tbl(company_id,
       vendor_num,
       vendor_name,
       address,
       bldg,
       region,
       province,
       municipality,
       product,
       others,
       comp_phone,
       fax_num,
       email,
       inactive_vendor)

       VALUES
       ('".$_SESSION['company_id']."',
       '".$this->edVendornum->Text."',
       '".$this->edVendorname->Text."',
       '".$this->edAddress->Text."',
       '".$this->edBldg->Text."',
       '".$this->cmbRegion->getItemIndex()."',
       '".$this->cmbProvince->getItemIndex()."',
       '".$this->cmbMunicipality->getItemIndex()."',
       '".$this->cmbProduct->getItemIndex()."',
       '".$this->edOtherProd->Text."',
       '".$this->edCompPhone->Text."',
       '".$this->edFax->Text."',
       '".$this->edEmail->Text."',
       '".$inactive."')") or die("INSERT VENDOR - ".mysql_error());

       //start auto-generate vin (note:INSERT STATEMENT)
       //die($this->hiddenExist->getValue());
       if($this->hiddenExist->getValue()=='0')
       {
       mysql_query("INSERT INTO lookup_vendor_count (company_id,total_vendor_count)
       VALUES ('".$_SESSION['company_id']."','1')")
       or die("INSERT COUNT ".mysql_error());
       }
       if($this->hiddenExist->getValue()=='1')
       {
       mysql_query("UPDATE lookup_vendor_count SET total_vendor_count='".$this->hiddenNewCount->getValue()."' WHERE
       company_id='".$_SESSION['company_id']."'")
       or die("UPDATE COUNT ".mysql_error());;
       }
       //end auto-generate vin

       //start contact list
       //mysql_insert_id()
        $result_vendor_id = mysql_query("SELECT last_insert_id(vendor_id) as last_id from vendor_tbl ORDER BY last_id DESC LIMIT 1");
        $row_vendor = mysql_fetch_array($result_vendor_id);
        $vendor_id = $row_vendor['last_id'];

        $query_up_cont = "UPDATE vendor_contact SET vendor_num='".$vendor_id."' WHERE company_id='".$_SESSION['company_id']."'
        AND edit_who='".$_SESSION['username']."' AND vendor_num=''";
        mysql_query($query_up_cont) or die("UPDATE CONTACTS ".mysql_error());
       //end contact list

       //redirect('vendor_list.php?company_id='.$_SESSION['company_id']);

       echo '<script language=javascript>
       //alert("Added contact successfully");
       //window.opener.JTContactList.location.reload();
       window.close();
       //window.close()
       //window.opener.document.getElementById("JTContactList").src="vendor_contact_list.php";
       </script>';

       }

}

global $application;

global $vendor_create;

//Creates the form
$vendor_create=new vendor_create($application);

//Read from resource file
$vendor_create->loadResource(__FILE__);

//Shows the form
$vendor_create->show();

?>